package com.sh.rabbitmq.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
@Slf4j
public class TtlQueueConfig {

    //TTL1延迟10秒队列
    public static final String QUEUE_A = "QA";

    //TTL1延迟40秒队列
    public static final String QUEUE_B = "QB";

    //死信队列
    public static final String QUEUE_D = "QD";

    //普通交换机
    public static final String EXCHANGE_X = "X";

    //死信交换机
    public static final String EXCHANGE_Y = "Y";

    @Bean("xExchange")
    public DirectExchange xExchange(){
        return new DirectExchange(EXCHANGE_X);
    }

    @Bean("yExchange")
    public DirectExchange yExchange(){
        return new DirectExchange(EXCHANGE_Y);
    }

    @Bean("queueA")
    public Queue queueA(){
        //Map<String,Object> arguments =  new HashMap<>();
        //arguments.put("x-dead-letter-exchange",EXCHANGE_Y);
        //arguments.put("x-dead-letter-routing-key","YD");
        //arguments.put("x-message-ttl",10000);

        return QueueBuilder.durable(QUEUE_A).deadLetterExchange(EXCHANGE_Y).deadLetterRoutingKey("YD").ttl(10000).build();
    }

    @Bean("queueB")
    public Queue queueB(){
        return QueueBuilder.durable(QUEUE_B).deadLetterExchange(EXCHANGE_Y).deadLetterRoutingKey("YD").ttl(30000).build();

    }

    @Bean("queueD")
    public Queue queueD(){
        return QueueBuilder.durable(QUEUE_D).build();

    }

    @Bean("queueABindingX")
    public Binding queueABindingX(@Qualifier("queueA") Queue queue,
                                  @Qualifier("xExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("XA");
    }

    @Bean("queueBBindingX")
    public Binding queueBBindingX(@Qualifier("queueB") Queue queue,
                                  @Qualifier("xExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("XB");
    }

    @Bean("queueDBindingY")
    public Binding queueDBindingY(@Qualifier("queueD") Queue queue,
                                  @Qualifier("yExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("YD");
    }
}
